package easy.id_0_100;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

public class hua9 {
    public static void main(String[] args) {
        String s1="9,3,5,-,2,*,+";
        System.out.println(calculate(s1));
    }

    // 待实现函数，在此函数中填入答题代码
    private static String calculate(String expression) {
        Deque<Integer> stack = new ArrayDeque<Integer>();
        char preSign = '+';
        int num = 0;
        int n = expression.length();
        for (int i = 0; i < n; ++i) {
            if (Character.isDigit(expression.charAt(i))) {
                num=num*10+expression.charAt(i)-'0';
            }
            if(!Character.isDigit(expression.charAt(i)) && expression.charAt(i)!=' '
            || i==n-1){
                switch (preSign){
                    case '+':
                        stack.push(num);
                        break;
                    case '-':
                        stack.push(-num);
                        break;
                    case  '*':
                        stack.push(stack.pop() * num);
                        break;
                    default:
                        if(num==0) return "error";
                        stack.push(stack.pop() / num);
                }
                preSign=expression.charAt(i);
                num=0;
            }
        }
        int ans = 0;
        while (!stack.isEmpty()) {
            ans += stack.pop();
        }
        return Integer.toString(ans);

    }

}
